﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Windows;
using uTorrentNetClient.ModelDesign.Helper;
using System.Threading;
using System.IO;

namespace uTorrentNetClient
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private bool clossing = false;
        private string processName = string.Empty;

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            clossing = true;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            processName = Process.GetCurrentProcess().ProcessName;

            var op = getRunningProcess();

            if (e.Args != null && e.Args.Length > 0)
            {
                if (op != null)
                    toPipe(e.Args[0]);
                else
                    StartupFile.File.Add(e.Args[0]);
            }

            if (op != null)
            {
                Process.GetCurrentProcess().Kill();
                return;
            }
            else
            {
                startPipeServer();
            }

            base.OnStartup(e);

            var b = new BootStrapper();
            b.Run();
        }

        private Process getRunningProcess()
        {
            var cp = Process.GetCurrentProcess();
            var op = Process.GetProcessesByName(cp.ProcessName);

            var p = op.FirstOrDefault(x => x.Id != cp.Id);

            return p;
        }

        private void startPipeServer()
        {
            var t = new Thread(checkPipeServer);
            t.IsBackground = true;
            t.Start();
        }
        
        private void toPipe(string fileName)
        {
            using (var p = new NamedPipeClientStream(".", "uTorrentClient", PipeDirection.Out))
            {
                p.Connect();
                using (StreamWriter sw = new StreamWriter(p))
                {
                    sw.AutoFlush = true;
                    sw.Write(fileName);
                }
                p.Close();
            }
        }

        private void checkPipeServer()
        {
            try
            {
                while (!clossing)
                {
                    using (var p = new NamedPipeServerStream("uTorrentClient", PipeDirection.In))
                    {

                        p.WaitForConnection();

                        using (var sr = new StreamReader(p))
                        {
                            string result;
                            result = sr.ReadToEnd();

                            StartupFile.File.Add(result);
                        }

                        p.Close();
                    }
                }
            }
            catch
            {
            }
        }
    }
}
